package com.sfl.api_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfl.api_backend.entity.Imgs;
import com.sfl.api_backend.entity.Task;
import com.sfl.api_backend.entity.TaskResult;
import com.sfl.api_backend.entity.pred.batchTaskPred;
import com.sfl.api_backend.mapper.ImgsMapper;
import com.sfl.api_backend.mapper.ModelsMapper;
import com.sfl.api_backend.mapper.ScriptsMapper;
import com.sfl.api_backend.mapper.TaskResultMapper;
import com.sfl.api_backend.service.ITaskResultService;
import com.sfl.api_backend.utils.Api.RestResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 任务结果 服务实现类
 * </p>
 *
 * @author szc
 * @since 2023-01-27
 */
@Service
public class TaskResultServiceImpl extends ServiceImpl<TaskResultMapper, TaskResult> implements ITaskResultService {

    @Resource
    private ModelsMapper modelsMapper;

    @Resource
    private ImgsMapper imgsMapper;

    @Resource
    private ScriptsMapper scriptsMapper;

    @Override
    public RestResult runTask(Task task) {
        task.setLastRunTime(LocalDateTime.now());
        // set batchTaskPred
        batchTaskPred batchTaskPred = new batchTaskPred();
        batchTaskPred.setUserId(task.getUserId());
        batchTaskPred.setTaskId(task.getTaskId());

        String modelPath = modelsMapper.selectById(task.getModelId()).getModelPath();

        Wrapper<Imgs> imgsWrapper = new LambdaQueryWrapper<>();
        ((LambdaQueryWrapper<Imgs>) imgsWrapper).eq(Imgs::getImgsetId, task.getImgsetId());
        List<String> imgPaths = new ArrayList<>();
        List<Integer> imgIds = new ArrayList<>();

        imgsMapper.selectList(imgsWrapper).forEach(img -> {
            imgPaths.add(img.getImgPath());
            imgIds.add(img.getImgId());
        });

//        batchTaskPred.setExScriptPath(scriptsMapper.selectById(task.getExScriptId()).getScriptPath());
//        batchTaskPred.setBackScriptPath(scriptsMapper.selectById(task.getBackScriptId()).getScriptPath());

        // TODO: 确定存储路径、如何确定类别数和颜色 -> {1.从前端传入(传入位置与是否保存)，2. 从数据库中读取(DefaultModel)}


        return null;
    }
}
